package com.xltt.vehicleControl.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.xltt.vehicleControl.R;

public class HttpResponseDialog extends Dialog {
    private HttpResponseDialog(Context context, int themeResId) {
        super(context, themeResId);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    public static class Builder {
        private View mLayout;
//        private ImageView mIcon;
        private TextView mMessage;
        private Button mButton;

        private View.OnClickListener mButtonClickListener;
        private HttpResponseDialog mDialog;
        public Builder(Context context) {
            mDialog = new HttpResponseDialog(context, R.style.Theme_AppCompat_Dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //加载布局文件
            mLayout = inflater.inflate(R.layout.operation_result, null, false);
            //添加布局文件到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                             ViewGroup.LayoutParams.WRAP_CONTENT));

//            mIcon = mLayout.findViewById(R.id.dialog_icon);
            mMessage = mLayout.findViewById(R.id.dialog_message);
            mButton = mLayout.findViewById(R.id.dialog_button);
         }

          /**
           * 通过 ID 设置 Dialog 图标
          */
          public Builder setIcon(int resId) {
//                 mIcon.setImageResource(resId);
              mButton.setBackgroundResource(resId);
                 return this;
          }

          /**
          * 用 Bitmap 作为 Dialog 图标
          */
          public Builder setIcon(Bitmap bitmap) {
//                 mIcon.setImageBitmap(bitmap);
                 return this;
          }


          /**
          * 设置 Message
          */
          public Builder setMessage(@NonNull String message) {
                 mMessage.setText(message);
                 return this;
          }

           /**
           * 设置按钮文字和监听
           */
          public Builder setButton(@NonNull String text, View.OnClickListener listener) {
                 mButton.setText(text);
                 mButtonClickListener = listener;
                 return this;
          }

          public HttpResponseDialog create() {
              mButton.setOnClickListener(view -> {
                      mDialog.dismiss();
                      if(mButtonClickListener != null){
                          mButtonClickListener.onClick(view);
                      }
                  });
              mDialog.setContentView(mLayout);
              mDialog.setCancelable(true);                //用户可以点击后退键关闭 Dialog
              mDialog.setCanceledOnTouchOutside(false);   //用户不可以点击外部来关闭 Dialog
              return mDialog;
             }
     }
}
